﻿using System;
using System.Collections.Generic;

using System.Text;
using ST.Model;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace ST.Dal
{
    public class ArchiveDal:AbstractDatabase<Archive>
    {
     
        public override string TableName
        {
            get { return "Archives"; }
        }

        public override string PrimaryKey
        {
            get { return "ArchiveID"; }
        }

        public override string Fields
        {
            get { return "ArchiveID,CategoryID,Title,SubTitle,Content,CreateDate,Author,ViewCount,CommentCount,IsPublish,IsDelete"; }
        }

        public override Archive PopulateObject(IDataReader reader)
        {
            Archive archive = new Archive();
            archive.ArchiveID = (System.Int32)reader["ArchiveID"];
            archive.CategoryID = (System.Int32)reader["CategoryID"];
            archive.Title = reader["Title"].ToString();
            archive.SubTitle = reader["SubTitle"] is DBNull ? string.Empty : reader["SubTitle"].ToString();
            archive.Content = reader["Content"].ToString();
            archive.CreateDate = Convert.ToDateTime(reader["CreateDate"]);
            archive.Author = reader["Author"].ToString();
            archive.ViewCount = (int)reader["ViewCount"];
            archive.CommentCount = (int)reader["CommentCount"];
            archive.IsDelete = (int)reader["IsDelete"] == 0 ? false : true;
            archive.IsPublish = (int)reader["IsPublish"] == 0 ? false : true;
            return archive;
        }
        public int AddArchive(Archive archive)
        {
            if (archive == null)
                throw new ArgumentNullException("add archive", "在新增文章时文章对象为空,请联系管理员！");
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(this.BuildInsertString("CategoryID", "Title", "SubTitle", "Content", "CreateDate", "Author","IsPublish"));
            db.AddInParameter(command, "@CategoryID", DbType.Int16, archive.CategoryID);
            db.AddInParameter(command, "@Title", DbType.String, archive.Title);
            db.AddInParameter(command, "@SubTitle", DbType.String, string.IsNullOrEmpty(archive.SubTitle)?DBNull.Value.ToString():archive.SubTitle);
            db.AddInParameter(command, "@Cotent", DbType.String, archive.Content);
            db.AddInParameter(command, "@CreateDate", DbType.DateTime, archive.CreateDate);
            db.AddInParameter(command, "@Author", DbType.String, archive.Author);
            db.AddInParameter(command, "@IsPublish", DbType.Boolean, archive.IsPublish);
            return command.ExecuteNonQuery();
        }
        /// <summary>
        /// update the archive
        /// </summary>
        /// <param name="archive"></param>
        /// <returns></returns>
        public bool UpdateArchive(Archive archive)
        {
            if(archive==null)
                throw new ArgumentNullException("update archive", "update the archive but the archive is null,please contact the administrator");
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(this.BuildUpdateString("CategoryID","Title","SubTitle","Content","Content","IsPublish","IsDelete"));
            db.AddInParameter(command, "@CategoryID", DbType.Int16, archive.CategoryID);
            db.AddInParameter(command, "@Title", DbType.String, archive.Title);
            db.AddInParameter(command, "@SubTitle", DbType.String, string.IsNullOrEmpty(archive.SubTitle) ? DBNull.Value.ToString() : archive.SubTitle);
            db.AddInParameter(command, "@Cotent", DbType.String, archive.Content);
            db.AddInParameter(command, "@IsPublish", DbType.Boolean, archive.IsPublish);
            db.AddInParameter(command, "@IsDelete", DbType.Boolean, archive.IsDelete);
            db.AddInParameter(command, "@ArchiveID", DbType.Int32, archive.ArchiveID);
            return command.ExecuteNonQuery() > 0;
        }
        /// <summary>
        /// add viewCount
        /// </summary>
        /// <param name="archive"></param>
        public void AddViewCount(Archive archive)
        {
            if (archive == null)
                throw new ArgumentNullException("update archive", "update the archive but the archive is null,please contact the administrator");
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(this.BuildUpdateString("ViewCount"));
            db.AddInParameter(command, "@ViewCount", DbType.Int32, archive.ViewCount);
            db.AddInParameter(command, "@ArchiveID", DbType.Int32, archive.ArchiveID);
            command.ExecuteNonQuery(); 
        }
        /// <summary>
        /// add commentcount
        /// </summary>
        /// <param name="archive"></param>
        public void AddCommentCount(Archive archive)
        {
            if (archive == null)
                throw new ArgumentNullException("update archive", "update the archive but the archive is null,please contact the administrator");
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(this.BuildUpdateString("CommentCount"));
            db.AddInParameter(command, "@CommentCount", DbType.Int32, archive.CommentCount);
            db.AddInParameter(command, "@ArchiveID", DbType.Int32, archive.ArchiveID);
            command.ExecuteNonQuery(); 
        }
        /// <summary>
        /// if category have archive
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public bool HaveArchiveByCategory(Category category)
        {
            Database db = CreateDatebase();
            DbCommand command = db.GetSqlStringCommand(string.Format("select count(1) from {0} where CategoryID=@CategoryID",TableName));
            db.AddInParameter(command, "@CategoryID", DbType.Int32, category.CategoryID);
            return (int)command.ExecuteScalar() > 0;
        }
        
        
    }
}
